//
//  TGMTableEmptyCell.m
//  TravelGuideMdd
//
//  Created by Song Xiaofeng on 13-6-28.
//  Copyright (c) 2013年 mafengwo.com. All rights reserved.
//

#import "TGMTableEmptyCell.h"

@interface TGMTableEmptyCell(){

}
@property(nonatomic,assign)float tipsStateDownY;

@end





@implementation TGMTableEmptyCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        
        _activityView = [TGMActivityIndicatorView defaultActivityIndicatorView];
        [_activityView stopAnimating];
        _activityView.hidden = YES;
        [self.contentView addSubview:_activityView];
        
        _tipsImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _tipsImageView.hidden = YES;
        [self.contentView addSubview:_tipsImageView];
        
        _tipsLabelView = [[UILabel alloc] initWithFrame:CGRectZero];
        _tipsLabelView.font = [UIFont systemFontOfSize:13];
        _tipsLabelView.textAlignment = NSTextAlignmentCenter;
        _tipsLabelView.numberOfLines = 0;
//        _tipsLabelView.textColor = SECOND_TEXT_COLOR;
        _tipsLabelView.backgroundColor = [UIColor clearColor];
        _tipsLabelView.hidden = YES;
        [self.contentView addSubview:_tipsLabelView];
        
        
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.backgroundColor = [UIColor whiteColor];
        
//        self.state = EmptyCellState_Loading;
    }
    return self;
}

- (void)refreshClick{
    
}

- (void)openSetting{
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    //这里面有点乱  因为首页做了一个无网络情况 这个页面目前只在首页 显示  为errorView
    if (self.state == EmptyCellState_Tips || self.state == EmptyCellState_Error)
    {
        _tipsImageView.hidden = NO;
        _tipsLabelView.hidden = NO;
        _activityView.hidden = YES;
        
        [_activityView stopAnimating];
        
        _tipsLabelView.text = self.emptyText;
        
        UIImage *image = self.emptyImage;

        NSInteger random = arc4random() % 5;
        if (self.emptyText == nil)
        {
            NSArray *textArray = [NSArray arrayWithObjects:EMPTY_TEXT_0, EMPTY_TEXT_1, EMPTY_TEXT_2, EMPTY_TEXT_3, EMPTY_TEXT_4, nil];
            _tipsLabelView.text = textArray[random];
        }

    
        _tipsImageView.image = image;
    
        NSDictionary *attributes = @{NSFontAttributeName: _tipsLabelView.font};
        CGSize tipsTxtSize = [_tipsLabelView.text boundingRectWithSize:CGSizeMake(self.width - 20, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
        int tipsContentHeight = image.size.height;
      
        BOOL showTip = YES;
        if ((int)tipsTxtSize.height > 0)
        {
            tipsContentHeight +=  IMAGE_TEXT_GAP + tipsTxtSize.height;
        }
        
        if (_jumpUrl && _jumpUrl.length > 0){
            //只要给了jumpurl 那么就要取actionstring 显示一个点击框
            tipsContentHeight = tipsContentHeight + ACTION_IMAGE_HEIGHT + ACTION_IMAGE_GAP;
            showTip = NO;
        }

        
        if(self.state == EmptyCellState_Tips || self.state == EmptyCellState_Error)
        {
            int startX = 0;
            int startY = (self.height - tipsContentHeight)/2;
            if(self.tipsStateDownY > 0.0f){
                startY = startY+self.tipsStateDownY;
            }
            startX = (self.height - image.size.width) * .5;
            _tipsImageView.frame = CGRectMake(startX, startY, image.size.width, image.size.height);
            
            if (showTip){
                _tipsLabelView.hidden = NO;
                startY += image.size.height + IMAGE_TEXT_GAP;
                startX = 0;
                _tipsLabelView.frame = CGRectMake(10, startY, self.width - 20, tipsTxtSize.height);
            }else{
                _tipsLabelView.hidden = YES;
                startY += image.size.height + ACTION_IMAGE_GAP;
                startX = 0;
            }
            
            if (self.state == EmptyCellState_Error){
                _tipsImageView.hidden = YES;
                _tipsImageView.hidden = YES;
                _tipsLabelView.hidden = YES;
            }else{
                _tipsLabelView.hidden = NO;
                _tipsImageView.hidden = NO;
                _tipsImageView.hidden = NO;
            }
        }else
        {
            int startX = 0;
            int startY = (self.frame.size.height - tipsContentHeight) * 0.5;
        
            startX = (self.width - image.size.width) * .5;
            _tipsImageView.frame = CGRectMake(startX, startY, image.size.width, image.size.height);

            if (showTip){
                _tipsLabelView.hidden = NO;
                startY += image.size.height + IMAGE_TEXT_GAP;
                startX = 0;
                _tipsLabelView.frame = CGRectMake(10, startY, self.width - 20, tipsTxtSize.height);
            }else{
                _tipsLabelView.hidden = YES;
                startY += image.size.height + ACTION_IMAGE_GAP;
                startX = 0;
            }
        }
    }
    else
    {
        _tipsImageView.hidden = YES;
        _tipsLabelView.hidden = YES;
        _activityView.hidden = NO;
        
        [_activityView startAnimating];
        _activityView.center = CGPointMake(self.frame.size.width * .5, self.frame.size.height * .5);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(void)tipsDownY:(float)y{
        self.tipsStateDownY = y;
}

@end
